/**
 * 版本管理相关类型定义
 * 与后端API完全对齐
 */

// 版本状态类型
export type VersionStatus = string

// 发布类型枚举
export enum ReleaseType {
  MAJOR = 'major',
  MINOR = 'minor',
  PATCH = 'patch',
  HOTFIX = 'hotfix'
}

// 环境枚举
export enum Environment {
  DEVELOPMENT = 'development',
  TESTING = 'testing',
  STAGING = 'staging',
  PRODUCTION = 'production'
}

// 发布状态枚举
export enum ReleaseStatus {
  PENDING = 'pending',
  IN_PROGRESS = 'in_progress',
  SUCCESS = 'success',
  FAILED = 'failed',
  ROLLED_BACK = 'rolled_back'
}

// 关联类型枚举
export enum RelationType {
  INCLUDES = 'includes',
  FIXES = 'fixes',
  IMPLEMENTS = 'implements',
  DEPENDS_ON = 'depends_on'
}

// 发布配置接口
export interface ReleaseConfig {
  auto_deploy?: boolean
  rollback_enabled?: boolean
  approval_required?: boolean
  notification_enabled?: boolean
  backup_enabled?: boolean
  health_check_enabled?: boolean
  deployment_strategy?: string
  max_rollback_time?: number
  notification_channels?: string[]
  approval_users?: string[]
}

// 发布元数据接口
export interface ReleaseMetadata {
  build_number?: string
  commit_hash?: string
  branch?: string
  build_url?: string
  deployment_url?: string
  rollback_url?: string
  health_check_url?: string
  documentation_url?: string
  changelog_url?: string
  [key: string]: any
}

// 版本基础接口
export interface VersionBase {
  name: string
  version_number: string
  description?: string
  project_id: string
  status: VersionStatus
  release_type: ReleaseType
  environment: Environment
  planned_release_date?: string
  actual_release_date?: string
  release_notes?: string
  release_config?: ReleaseConfig
  metadata?: ReleaseMetadata
}

// 版本创建接口
export interface VersionCreate extends VersionBase {}

// 版本更新接口
export interface VersionUpdate extends Partial<VersionBase> {}

// 版本接口
export interface Version extends VersionBase {
  id: string
  created_by: string
  created_at: string
  updated_at: string
}

// 版本统计接口
export interface VersionWithStats extends Version {
  project_name: string
  task_count: number
  defect_count: number
  release_count: number
}

// 版本详情响应接口
export interface VersionDetailResponse extends Version {
  project_name: string
  created_by_name: string
  task_count: number
  defect_count: number
  requirement_count: number
  release_history: ReleaseRecord[]
}

// 版本比较结果接口
export interface VersionCompareResult {
  source_version: Version
  target_version: Version
  differences: {
    tasks: {
      added: any[]
      removed: any[]
      modified: any[]
    }
    defects: {
      added: any[]
      removed: any[]
      resolved: any[]
    }
    requirements: {
      added: any[]
      removed: any[]
      modified: any[]
    }
  }
  summary: {
    total_changes: number
    task_changes: number
    defect_changes: number
    requirement_changes: number
  }
}

// 版本关联信息接口
export interface VersionAssociation {
  id: string
  version_id: string
  entity_type: 'task' | 'defect' | 'requirement'
  entity_id: string
  entity_title: string
  entity_status: string
  associated_at: string
  associated_by: string
}

// 版本任务关联接口
export interface VersionTaskRelation {
  id: string
  version_id: string
  task_id: string
  task_title: string
  task_status: string
  relation_type: RelationType
  associated_at: string
  associated_by: string
}

// 版本任务关联创建接口
export interface VersionTaskRelationCreate {
  task_id: string
  relation_type: RelationType
  notes?: string
}

// 版本缺陷关联接口
export interface VersionDefectRelation {
  id: string
  version_id: string
  defect_id: string
  defect_title: string
  defect_status: string
  relation_type: RelationType
  associated_at: string
  associated_by: string
}

// 版本缺陷关联创建接口
export interface VersionDefectRelationCreate {
  defect_id: string
  relation_type: RelationType
  notes?: string
}

// 版本需求关联接口
export interface VersionRequirement {
  id: string
  version_id: string
  requirement_id: string
  requirement_title: string
  requirement_status: string
  relation_type: RelationType
  associated_at: string
  associated_by: string
}

// 版本需求关联创建接口
export interface VersionRequirementCreate {
  requirement_id: string
  relation_type: RelationType
  notes?: string
}

// 发布请求接口
export interface ReleaseRequest {
  release_notes?: string
  environment: Environment
  release_config?: ReleaseConfig
  metadata?: ReleaseMetadata
}

// 回滚请求接口
export interface RollbackRequest {
  rollback_reason: string
  target_version_id?: string
  rollback_config?: any
}

// 发布记录接口
export interface ReleaseRecord {
  id: string
  version_id: string
  release_date: string
  release_notes?: string
  environment: Environment
  released_by: string
  released_by_name?: string
  status: ReleaseStatus
  rollback_reason?: string
  deployment_log?: string
  metadata?: ReleaseMetadata
}

// 版本统计报告接口
export interface VersionStatistics {
  total_versions: number
  versions_by_status: Record<VersionStatus, number>
  versions_by_project: Array<{
    project_id: string
    project_name: string
    version_count: number
  }>
  release_metrics: {
    total_releases: number
    successful_releases: number
    failed_releases: number
    rollback_count: number
    avg_release_time: number
  }
  quality_metrics: {
    avg_defect_count: number
    avg_task_completion_rate: number
    avg_test_coverage: number
  }
}

// 质量指标接口
export interface QualityMetrics {
  defect_density: number
  test_coverage: number
  code_quality_score: number
  performance_score: number
  security_score: number
  maintainability_score: number
}

// 批量操作结果接口
export interface BulkOperationResult {
  success_count: number
  failed_count: number
  total_count: number
  failed_items?: Array<{
    id: string
    error: string
  }>
}

// 影响分析结果接口
export interface ImpactAnalysis {
  version_id: string
  direct_impact: {
    tasks: number
    defects: number
    requirements: number
  }
  indirect_impact: {
    dependent_versions: string[]
    affected_projects: string[]
    risk_level: 'low' | 'medium' | 'high'
  }
  recommendations: string[]
}

// 变更历史记录接口
export interface ChangeHistory {
  id: string
  version_id: string
  change_type: string
  change_description: string
  changed_by: string
  changed_by_name: string
  changed_at: string
  old_value?: any
  new_value?: any
}

// 发布计划接口
export interface ReleasePlan {
  id: string
  name: string
  description?: string
  version_ids: string[]
  planned_date: string
  environment: Environment
  status: 'draft' | 'pending_approval' | 'approved' | 'rejected' | 'executed'
  created_by: string
  created_at: string
  approved_by?: string
  approved_at?: string
  approval_comments?: string
}

// 审批数据接口
export interface ApprovalData {
  approved: boolean
  comments?: string
  conditions?: string[]
}

// 性能统计接口
export interface PerformanceStats {
  query_performance: {
    avg_response_time: number
    total_queries: number
    slow_queries: number
  }
  resource_usage: {
    memory_usage: number
    cpu_usage: number
    disk_usage: number
  }
  cache_performance: {
    hit_rate: number
    miss_rate: number
    eviction_rate: number
  }
}

// 版本选项接口（用于选择器）
export interface VersionOption {
  id: string
  version_number: string
  name: string
  status: VersionStatus
  release_type: ReleaseType
  project_id: string
  project_name?: string
  created_at: string
}

// ========== 版本状态流转相关类型 ==========

// 版本完成度接口
export interface VersionCompletion {
  task_completion: number
  defect_completion: number
  requirement_completion: number
  test_completion: number
  overall_completion: number
  details: {
    tasks: { total: number; completed: number }
    defects: { total: number; resolved: number }
    requirements: { total: number; completed: number }
    tests: { total: number; passed: number; total_run: number }
  }
}

// 版本流转规则接口
export interface VersionTransitionRule {
  from_status: string
  to_status: string
  name: string
  auto_trigger: boolean
  require_approval: boolean
  require_reason: boolean
}

// 版本流转准备情况接口
export interface VersionTransitionReadiness {
  current_status: string
  completion_data?: VersionCompletion
  readiness: Array<{
    target_status: string
    rule_name: string
    is_ready: boolean
    unmet_conditions: string[]
    auto_trigger: boolean
    require_approval: boolean
  }>
}

// 版本状态流转审批请求接口
export interface VersionTransitionApproval {
  id: string
  version_id: string
  version_name?: string
  from_status: string
  to_status: string
  reason?: string
  status: 'pending' | 'approved' | 'rejected' | 'cancelled'
  requester_id: string
  requested_at: string
  approval_flow_type: 'simple' | 'multi' | 'auto'
  approvers: Array<{
    user_id: string
    role: string
    name: string
  }>
  current_approver_index?: number
  approved_by?: string
  approved_at?: string
  approval_notes?: string
  rejection_reason?: string
}

// 版本状态流转申请数据
export interface VersionTransitionRequest {
  to_status: string
  reason?: string
  approval_flow_type?: 'simple' | 'multi' | 'auto'
}

// 版本状态流转数据
export interface VersionTransitionData {
  new_status: string
  reason?: string
  comment?: string
}

// 审批操作数据
export interface ApprovalActionData {
  approved: boolean
  notes?: string
}